

class PlaceController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ placeInstanceList: Place.list( params ), placeInstanceTotal: Place.count() ]
    }

    def show = {
        def placeInstance = Place.get( params.id )

        if(!placeInstance) {
            flash.message = "Place not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ placeInstance : placeInstance ] }
    }

    def delete = {
        def placeInstance = Place.get( params.id )
        if(placeInstance) {
            try {
                placeInstance.delete()
                flash.message = "Place ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Place ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Place not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def placeInstance = Place.get( params.id )

        if(!placeInstance) {
            flash.message = "Place not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ placeInstance : placeInstance ]
        }
    }

    def update = {
        def placeInstance = Place.get( params.id )
        if(placeInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(placeInstance.version > version) {
                    
                    placeInstance.errors.rejectValue("version", "place.optimistic.locking.failure", "Another user has updated this Place while you were editing.")
                    render(view:'edit',model:[placeInstance:placeInstance])
                    return
                }
            }
            placeInstance.properties = params
            if(!placeInstance.hasErrors() && placeInstance.save()) {
                flash.message = "Place ${params.id} updated"
                redirect(action:show,id:placeInstance.id)
            }
            else {
                render(view:'edit',model:[placeInstance:placeInstance])
            }
        }
        else {
            flash.message = "Place not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def placeInstance = new Place()
        placeInstance.properties = params
        return ['placeInstance':placeInstance]
    }

    def save = {
        def placeInstance = new Place(params)
        if(!placeInstance.hasErrors() && placeInstance.save()) {
            flash.message = "Place ${placeInstance.id} created"
            redirect(action:show,id:placeInstance.id)
        }
        else {
            render(view:'create',model:[placeInstance:placeInstance])
        }
    }
}
